package com.yztc.yidoutang.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.yidoutang.R;
import com.yztc.yidoutang.bean.PicsBean;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by sqq on 16/9/20.
 */
public class PicRecyclerAdapter extends RecyclerView.Adapter<PicRecyclerAdapter.MyViewHolder> {
    private Context context;
    private List<PicsBean> list;
    private OnItemClickListener listener;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public PicRecyclerAdapter(Context context, List<PicsBean> list) {
        this.context = context;
        this.list = list;

    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.recycleitem_normal, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        Glide.with(context).load(list.get(position).getSmallImage()).into(holder.image);
        Glide.with(context).load(list.get(position).getUserPic()).into(holder.header);
        holder.title.setText(list.get(position).getCaseTitle());
        holder.name.setText(list.get(position).getUserName());
        holder.num.setText(list.get(position).getFavtimes());
        if (listener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    listener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    listener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView title;
        CircleImageView header;
        TextView name;
        CheckBox collect;
        TextView num;

        public MyViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.find_recycleitem_iv_image);
            title = (TextView) itemView.findViewById(R.id.find_recycleitem_tv_title);
            header = (CircleImageView) itemView.findViewById(R.id.find_recycleitem_iv_header);
            name = (TextView) itemView.findViewById(R.id.find_recycleitem_tv_name);
            collect = (CheckBox) itemView.findViewById(R.id.find_recycleitem_cb_collect);
            num = (TextView) itemView.findViewById(R.id.find_recycleitem_tv_num);
        }
    }

}
